package kr.co.insoft.core.service.callback.impl;

import kr.co.insoft.core.model.callback.CallBackParamModel;
import kr.co.insoft.core.model.callback.CallBackResultModel;
import kr.co.insoft.core.repository.eventchain.EventLogRepository;
import kr.co.insoft.core.service.callback.CallBackService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

@Service
public class CallBackServiceImpl implements CallBackService {

	private static final Logger CallBackServiceImplLog = LoggerFactory
			.getLogger(CallBackServiceImpl.class);

	final String NOT_ALLOWED_IP = "127.0.0.1";

	@Autowired
	EventLogRepository eventLogRepository;

	@Override
	@Transactional
	public CallBackResultModel doDistribution(
			CallBackParamModel callBackParamModel) throws ServiceException {
		try {
			Assert.notNull(callBackParamModel.getCallbackVersion());
			Assert.notNull(callBackParamModel.getCallbackCommand());
			Assert.notNull(callBackParamModel.getCallbackInitiator());
			Assert.notNull(callBackParamModel.getCallbackSystem());
			Assert.notNull(callBackParamModel.getCallbackType());

			if (!StringUtils.hasLength(callBackParamModel.getIsReboot()))
				callBackParamModel.setIsReboot("N");

			Assert.isTrue(StringUtils.hasLength(callBackParamModel
					.getInstanceId())
					|| StringUtils.hasLength(callBackParamModel.getInstanceIp()));

			Assert.isTrue(!NOT_ALLOWED_IP.equals(callBackParamModel
					.getInstanceIp()));
		} catch (IllegalArgumentException e) {
			if (CallBackServiceImplLog.isDebugEnabled()) {
				CallBackServiceImplLog.debug("CallBack In Data : {}",
						callBackParamModel);
			}
			throw new ServiceException(ExceptionCode.ARGUMENT_ERROR);
		}

		CallBackDataConvert callBackDataConvert = new CallBackDataConvert(
				callBackParamModel.getCallbackData());
		callBackParamModel
				.setCallBackDataModel(callBackDataConvert.doConvert());

		try {
			eventLogRepository.createEventLog(callBackParamModel
					.getCallBackDataModel());
			switch (callBackParamModel.getCallbackSystem()) {
			case "sa":
				return sa(callBackParamModel);
			case "sp":
				return sp(callBackParamModel);
			default:
				throw new ServiceException(ExceptionCode.NOT_FOUND_EXCEPTION);
			}
		} catch (RepositoryException e) {
			if (CallBackServiceImplLog.isDebugEnabled()) {
				CallBackServiceImplLog.debug("callBackParamModel : {}",
						callBackParamModel);
			}
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		} catch (ServiceException e) {
			throw e;
		}
	}

	private CallBackResultModel sp(CallBackParamModel callBackParamModel)
			throws ServiceException {
		switch (callBackParamModel.getCallbackType()) {
		case "cmd":
		case "event":
		default:
			throw new ServiceException(ExceptionCode.NOT_SUPPROTED_EXCEPTION);
		case "report":

		}
		return null;
	}

	private CallBackResultModel sa(CallBackParamModel callBackParamModel)
			throws ServiceException {
		switch (callBackParamModel.getCallbackType()) {
		case "cmd":
		case "report":
		default:
			throw new ServiceException(ExceptionCode.NOT_SUPPROTED_EXCEPTION);
		case "event":
			break;

		}
		return null;
	}

}
